import { Observer, getTimerFun, twowayBind } from "./index";
import Watcher from "./watcher";
import { deepCopy } from "./deep-copy";
let log = console.log;
let pretty = (v) => {
    log(JSON.stringify(v, null, 2));
};
let timeout = getTimerFun();

let data_1 = { a: { b: 10 } };
let data_2 = { b: 10 };

let data_1_root = { data: data_1 };
let data_2_root = { data: data_2 };

let ob_1 = new Observer(data_1_root);
let ob_2 = new Observer(data_2_root);

let stack = [deepCopy(data_1_root)];
let watcher_1 = new Watcher(data_1_root, 'data', (newV, oldV) => {
    stack.push(deepCopy(data_1_root));
}, { deep: true });
ob_1.addWatcher(watcher_1);

let dis = twowayBind(data_1, 'a.b', data_2, 'b');

timeout(() => {
    data_1.a.b = 100;
}, 100);
timeout(() => {
    log(data_1.a.b);
    log(data_2.b);
    dis();
    data_1.a.b = 150;
}, 1000);

timeout(() => {
    log(data_1.a.b);
    log(data_2.b);
}, 1500);

timeout(() => {
    pretty(stack);
}, 2000);

let data_3 = { b: 10, c: 10, [Observer.onlyKey]: ['b'] };
let ob_3 = new Observer(data_3);

// tslint:disable-next-line:no-unused-expression
new Watcher(data_3, 'b', (newV, oldV) => {
    log(newV, oldV);
});
data_3.b = 100;

// tslint:disable-next-line:no-unused-expression
new Watcher(data_3, 'c', (newV, oldV) => {
    log(newV, oldV);
});
data_3.c = 100;
